﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 暑假作业
{
    public partial class stockInfo : Form
    {
        public stockInfo()
        {
            InitializeComponent();
        }
        //关闭回到主界面
        private void stockInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }
        //展示数据
        private void stockInfo_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            var sql = "select g.GoodsId,g.GName,g.UnitPrice,d.DName,s.StorafeNum,g.Manufactrue from Goods as g left join StorageInfo as s on g.GoodsId = s.GoodsId left join Depot as d on s.DepotId = d.id";
            DataSet ds = DBHelper.show(sql);
            if (ds.Tables.Count>0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("查询失败");
            }
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
           
            if(comboBox1.SelectedIndex!=0 && comboBox2.SelectedIndex==0)
            {
                var sql = $"select g.GoodsId,g.GName,g.UnitPrice,d.DName,s.StorafeNum,g.Manufactrue from Goods as g left join StorageInfo as s on g.GoodsId = s.GoodsId left join Depot as d on s.DepotId = d.id where gname='{comboBox1.Text}'";
                DataSet ds = DBHelper.show(sql);
                 if (ds.Tables.Count > 0)
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("查询失败");
                }
            }
            if (comboBox1.SelectedIndex == 0 && comboBox2.SelectedIndex != 0)
            {
                var sql = $"select g.GoodsId,g.GName,g.UnitPrice,d.DName,s.StorafeNum,g.Manufactrue from Goods as g left join StorageInfo as s on g.GoodsId = s.GoodsId left join Depot as d on s.DepotId = d.id where dname='{comboBox2.Text}'";
                DataSet ds = DBHelper.show(sql);
                if (ds.Tables.Count > 0)
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("查询失败");
                }
            }
            if (comboBox1.SelectedIndex == 0 && comboBox2.SelectedIndex == 0)
            {
                stockInfo_Load(null, null);
            }
            else if(comboBox1.SelectedIndex != 0 && comboBox2.SelectedIndex != 0)
            {
                var sql = $"select g.GoodsId,g.GName,g.UnitPrice,d.DName,s.StorafeNum,g.Manufactrue from Goods as g left join StorageInfo as s on g.GoodsId = s.GoodsId left join Depot as d on s.DepotId = d.id where dname='{comboBox2.Text}' and gname='{comboBox1.Text}'";
                DataSet ds = DBHelper.show(sql);
                if (ds.Tables.Count > 0)
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("请选择正确信息");
                }
            }
        }
    }
}
